﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace iArtis.Extensions
{
    public static class AppExtensions
    {
        public static string AppPath()
        {
            string port = HttpContext.Current.Request.ServerVariables["SERVER_PORT"];
            if (port == null || port == "80" || port == "443")
                port = "";
            else
                port = ":" + port;
            string protocol = HttpContext.Current.Request.ServerVariables["SERVER_PORT_SECURE"];
            if (protocol == null || protocol == "0")
                protocol = "http://";
            else
                protocol = "https://";
            string ret = protocol + HttpContext.Current.Request.ServerVariables["SERVER_NAME"] +
                         port +
                         (HttpContext.Current.Request.ApplicationPath.Equals("/")
                              ? ""
                              : HttpContext.Current.Request.ApplicationPath);

            return ret;
        }

        public static string AppPath(this HtmlHelper htmlHelper)
        {
            return AppPath();
        }

        public static string ActionLinkImage(this HtmlHelper htmlHelper, string altImageText, string imagePath,
                                             string actionName, string controllerName, object routeValue,
                                             IDictionary<string, object> htmlAttributes)
        {
            const string linkTextReplacement = "F14E9A0EB8C4";
            string actionLink = LinkExtensions.ActionLink(htmlHelper, linkTextReplacement, actionName, controllerName,
                                                          routeValue, htmlAttributes);

            return actionLink.Replace(linkTextReplacement,
                                      String.Format(@"<img src='{0}' alt='{1}'/>", imagePath, altImageText));
        }

        public static string ActionLink(this HtmlHelper htmlHelper, string linkText, string actionName,
                                        string controllerName, string anchor)
        {
            return LinkExtensions.ActionLink(htmlHelper, linkText, actionName, controllerName, null, null, anchor, null,
                                             null);
        }
    }
}